//
//  ViewController.m
//  YZYSDKDemo
//
//  Created by 赵隆杰 on 2024/6/12.
//

#import "ViewController.h"
#import "JHLCoreEntity.h"
#import "Masonry.h"
#import "ZCFunDataFormat.h"

@interface ViewController ()<JHLCoreEntityDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[JHLCoreEntity sharedInstance] qpjh_setStartLoginListener:self];
        
    //设置几个button测试用
    [self initSetupUI];
}

#pragma mark - JHLCoreEntityDelegate
//聚合渠道:登录回调
- (void)qpjh_LightGetChannelLoginCallBack:(NSDictionary *)result {
    NSString *super_user_id = [NSString stringWithFormat:@"%@",result[@"super_user_id"]];
    NSString *serviceToken = [NSString stringWithFormat:@"%@",result[@"token"]];
    
    NSLog(@"super_user_id == %@, serviceToken = %@ ",super_user_id,serviceToken);

    NSDictionary *loginDic = [NSDictionary dictionaryWithObjectsAndKeys:super_user_id, @"super_user_id", serviceToken,@"token",nil];
#warning - 用户效验接口 请在服务器做校验验证用户信息❗️❗️❗️
//        [ZCNetworkManager postWithURL:@"https://supersdk.7pa.com/login/checkuserinfo" Params:loginDic success:^(id responseObject) {
//
//        } failure:^(NSString *error) {
//
//        }];
}

/**
 切换/退出账号回调
 此回调表示平台已完成退出，请勿再调用平台退出接口，否则会造成死循环；
 请在此方法进行游戏退出操作
 */
- (void)logOutCallBack:(NSString *)result {
    NSLog(@"切换/退出账号回调,%@", result);
}

/**
 上报角色回调
 */
- (void)getUpdateRoleCallBack:(NSString *)result {
    NSLog(@"上报角色回调,%@", result);
}

//支付成功回调
- (void)getPaySuccessCallBack:(NSDictionary *)result {
    NSLog(@"支付成功回调:%@",result);
}

//支付取消回调
- (void)getPayCancelCallBack:(NSDictionary *)result {
    NSLog(@"支付取消回调:%@",result);
}
    
//支付失败回调
- (void)getPayFailCallBack:(NSDictionary *)result {
    NSLog(@"支付失败回调:%@",result);
}

//选择区服【停止新增用户】回调: 当result为1可进入该区服,当result为其他值时,SDK里有错误提示.研发不做任何处理.
- (void)canEnterListenCallBack:(NSString *)result {
    
}

#pragma mark - 游戏登录
- (void)loginButtonClick {
    [[JHLCoreEntity sharedInstance] qpjh_lightLoginStart];
}

#pragma mark - 提交角色
- (void)submitRoleButtonClick {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    
    NSString *role_type = @"enterserver";//角色上报类型: createrole 创建角色; enterserver 进入服务器调用; levelup 升级角色; loginout 退出登录;
    NSString *login_time = @"";
    NSString *loginout_time = @"";
    NSString *reg_time = @"";
    NSString *update_time = @"";
    if ([role_type isEqualToString:@"enterserver"]) {
        login_time = timeString;
    } else if ([role_type isEqualToString:@"createrole"]) {
        reg_time = timeString;
    } else if ([role_type isEqualToString:@"levelup"]) {
        update_time = timeString;
    } else {
        loginout_time = timeString;
    }
    
    //1910 17673046383 123456
    NSDictionary *dic1 = @{
                            @"role_type":@"enterserver",
                            @"service_id":@"291",
                            @"service_name":@"养老院",
                            @"role_id": @"7573197553348315474",
                            @"role_name":@"张志学",
                            @"role_level":@"2",
                            @"power_value":@"780",
                            @"describe":@"",
                            @"money":@"0",
                            @"experience":@""
                            };
    NSError* error1;
    NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dic1 options:NSJSONWritingPrettyPrinted error:&error1];
    NSString* roleData1 = [[NSString alloc] initWithData:jsonData1 encoding: NSUTF8StringEncoding];
    
    [[JHLCoreEntity sharedInstance] qpjh_superRoleUpdate:roleData1];
}

#pragma mark - 支付
- (void)payButonClick {
    
    //1910 17673046383 123456
    NSDictionary *dic1 = @{
                           @"good_id":@"com.xlyou.news1",//商品ID，以苹果后台生成为准
                           @"good_name":@"￥100元礼包",         //商品名
                           @"game_order_sn":@"11639143",  //订单号
                           @"money":@"1",               //订单金额 (单位:元)
                           @"remark":@"11639143"          //扩展参数 (选填)
                           };
    NSError* error1;
    NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dic1 options:NSJSONWritingPrettyPrinted error:&error1];
    NSString* roleData1 = [[NSString alloc] initWithData:jsonData1 encoding: NSUTF8StringEncoding];
    [[JHLCoreEntity sharedInstance] qpjh_superOrder:roleData1];
}

#pragma mark - 退出登录
- (void)logoutButtonClick {
    [[JHLCoreEntity sharedInstance] qpjh_logout];
}

#pragma mark - 删除账号
- (void)accountDeleteClick {
    [[JHLCoreEntity sharedInstance] qpjh_logoutForAccountDelete];
}

#pragma mark - 打开小程序 入口在游戏页面研发添加的图标
- (void)miniProgramsClick {
    [[JHLCoreEntity sharedInstance] qpjh_jump_miniPrograms];
}

- (void)buttonClick:(UIButton *)button {
    NSInteger buttonTag = button.tag;
    switch (buttonTag) {
        case 0: {
            [self loginButtonClick];
        }
            break;
        case 1: {
            [self submitRoleButtonClick];
        }
            break;
        case 2: {
            [self payButonClick];
        }
            break;
        case 3: {
            [self logoutButtonClick];
        }
            break;
        case 4: {
            [self miniProgramsClick];
        }
            break;
        case 5: {
            [self accountDeleteClick];
        }
            break;
        default:
            break;
    }
}

- (void)initSetupUI {
    
    for (NSUInteger i = 0; i < [self buttonTitles].count; i++) {
        [self createButtonWithType:i];
    }
    
}

- (UIButton *)createButtonWithType:(NSUInteger)tag {
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5;
    button.backgroundColor = [UIColor systemFillColor];
    [button setTitle:[self buttonTitles][tag] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor labelColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    CGFloat buttonW = 200.0;
    CGFloat buttonH = 50.0;
    CGFloat space = 50.0;
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(buttonH);
        make.width.mas_equalTo(buttonW);

        if ([ZCFunDataFormat GetIsLandscape]) {
            make.left.mas_equalTo(self.view.safeAreaInsets.left + 100 + (buttonW + space) * (tag % 2));
            make.top.mas_equalTo(self.view.safeAreaInsets.top + 40 + (buttonH + space) * (tag / 2));
        } else {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.view.safeAreaInsets.top + 120 + (buttonH + space) * tag);
        }
    }];
    
    return button;
}

- (NSArray *)buttonTitles {
    return @[
        @"游戏登录",
        @"提交角色",
        @"支付测试",
        @"退出登录",
        @"跳转小程序",
        @"删除账号",
    ];
}

@end
